/**
 * 用户组权限列表选择
 */

var viewer;

//页面载入初始化
$( "body" ).ready( function( e )
{
	viewer = new ImageViewer();
	viewer.cur = power;
	viewer.init();
} );

//窗口大小改变
$( window ).resize( function( e )
{
	if( viewer.visibled() )
	{
		viewer.resize();
		viewer.relocate();
	}
} );

/**
 * 外部调用接口
 * 
 * @param url
 * @param baseWidth
 * @param baseHeight
 */
function imgv( url, baseWidth, baseHeight )
{
	viewer.open( url, baseWidth, baseHeight );
}

function ImageViewer()
{
	this.cur = null;
	this.html = "<div id='__DIV_IMAGEVIEWER_MASK' style='display:block;position:absolute;z-index:8999;background-color:#000;filter:alpha(opacity=20);opacity:0.2;'></div><div id='__DIV_IMAGEVIEWER' style='position:absolute;z-index:9000;'></div><div id='__DIV_IMAGEVIEWER_CONTROL' style='position:absolute;z-index:9000;'><a id='btnClose'>[ 关闭 ]</a><a id='btnRawSize'>[ 查看原始大小图片 ]</a></div>";

	// 变量
	var $box;
	var $mask;
	var $control;

	var $btnClose;
	var $btnRawSize;

	// 初始化
	this.init = function()
	{
		$( "body" ).append( this.html );
		$box = $( "#__DIV_IMAGEVIEWER" );
		$mask = $( "#__DIV_IMAGEVIEWER_MASK" );
		$control = $( "#__DIV_IMAGEVIEWER_CONTROL" );

		$btnClose = $control.find( "a[id=btnClose]" );
		$btnRawSize = $control.find( "a[id=btnRawSize]" );

		$btnClose.click( this.close );
		$btnRawSize.attr( { href: "", target: "_blank" } );

		$box.find( "#btn_cancel" ).click( { cur: this.cur }, function( e )
		{
			var pcur = e.data.cur;
			pcur.close();
		} );

		$box.hide();
		$mask.hide();
		$control.hide();
	};

	// 是否显示
	this.visibled = function()
	{
		return $box.visible();
	};

	// 显示单条信息
	this.open = function( url, baseWidth, baseHeight )
	{
		$box.html( "<img rel='" + baseWidth + ":" + baseHeight + "' src='" + url + "'/>" );
		$btnRawSize.attr( "href", url );
		$box.find( "img" ).bind( "dblclick", this.close );

		this.resize();
		this.relocate();
		$control.fadeIn( 150 );
		$box.fadeIn( 150 );
		$mask.fadeIn( 150 );
	};

	this.resize = function()
	{
		var bw = $( "body" ).width();
		var bh = $( "body" ).height();
		var img = $box.find( "img" );

		// 限制图片保持在窗口以内
		var mw = bw * 0.9;
		var mh = bh * 0.9;
		var wh = img.attr( 'rel' ).split( ":" );
		var dw = wh[ 0 ];
		var dh = wh[ 1 ];

		if( dw > mw )
		{
			dw = mw;
			dh = ( mw / wh[ 0 ] ) * wh[ 1 ];
		}

		if( dh > mh )
		{
			dh = mh;
			dw = ( mh / wh[ 1 ] ) * wh[ 0 ];
		}
		img.attr( { width: dw, height: dh } );
	};

	this.relocate = function()
	{
		var bw = $( "body" ).width();
		var bh = $( "body" ).height();
		var w = $box.width();
		var h = $box.height();

		$mask.width( bw );
		$mask.height( bh );
		$mask.move( 0, 0 );
		$box.move( ( bw - w ) / 2, ( bh - h ) / 2 * 0.8 );

		// 控制栏
		var cw = $control.width();
		var ch = $control.height();
		$control.move( ( bw - cw ) / 2, bh - ch - 20 );
	};

	this.close = function()
	{
		$control.fadeOut( 150 );
		$box.fadeOut( 150 );
		$mask.fadeOut( 150 );
	};
}
